/*
 * Copyright 2016 Yan Zhenjie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hxkcloud.patient.aerotel.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.yanzhenjie.recyclerview.swipe.SwipeMenuAdapter;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.aerotel.AerotelActivity;
import com.hxkcloud.patient.aerotel.domain.health.AerotelECGPdu;
import com.hxkcloud.patient.aerotel.listener.OnItemClickListener;
import com.hxkcloud.patient.aerotel.util.DateUtil;

import java.text.ParseException;
import java.util.List;

/**
 * Created by YOLANDA on 2016/7/22.
 */
public class MenuAdapter extends SwipeMenuAdapter<MenuAdapter.DefaultViewHolder> {

    private List<AerotelECGPdu> ecgs;
    private AerotelActivity mainactivity = null;
    private OnItemClickListener mOnItemClickListener;

    public MenuAdapter(Context context, List<AerotelECGPdu> ecgs) {
        mainactivity = (AerotelActivity) context;
        this.ecgs = ecgs;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return ecgs == null ? 0 : ecgs.size();
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.ecg_list_item, parent, false);
    }

    @Override
    public MenuAdapter.DefaultViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new DefaultViewHolder(realContentView);
    }

    @Override
    public void onBindViewHolder(MenuAdapter.DefaultViewHolder holder, int position) {
        holder.setData(ecgs.get(position));
        holder.setOnItemClickListener(mOnItemClickListener);
    }

    class DefaultViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        //        TextView tvTitle;
        ImageView imgv;
        TextView tv;
        TextView tvDevice;
        OnItemClickListener mOnItemClickListener;

        public DefaultViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
//            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            imgv = (ImageView) itemView.findViewById(R.id.imgv_preview);
            tv = (TextView) itemView.findViewById(R.id.tv_record_datetime);
            tvDevice = (TextView) itemView.findViewById(R.id.tvDevice);
        }

        public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
            this.mOnItemClickListener = onItemClickListener;
        }

        public void setData(AerotelECGPdu ecg) {
            Bitmap thumb = ecg.getThumbImage(mainactivity);
            if (thumb != null) {
                imgv.setImageBitmap(thumb);
            }
            String date = ecg.createdDate;
            try {
                date = DateUtil.normalize(date, "yyyy-MM-dd HH:mm", "yyyy-MM-dd h:mm aa");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            tv.setText(date.toUpperCase());
            tvDevice.setText(ecg.devicename);
//            this.tvTitle.setText(title);
        }

        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(getAdapterPosition());
            }
        }
    }

}
